var express = require('express');
var router = express.Router();
var formidable = require('formidable');
var util = require('util');
var path =  require('path');
const {resolve} = require('path')
var fs = require("fs");
var exprienceModel = require('../tool/model/exprience.js');

router.get('/', (req, res, next)=> {
        res.render('exprience', {
          id: req.cookies['id']
        })
});
router.post('/exprienceAction',async function(req, res, next) {
  let ctime = Date.now()
  let insertData = req.body; 
  console.log(insertData)
  var article = new exprienceModel ({
        projectMark:insertData.projectMark,//文章id  
        projectTitle:insertData.projectTitle,//文章标题
        projectDesc:insertData.projectDesc,//文章简介
        ctime:ctime,
        startTime:insertData.startTime,//开始时间
        endTime:insertData.endTime,//结束时间
        status:insertData.status//文章状态，默认为发布(0为保存)
    })
    await article.save(function(err){
      if(err){
          res.send({
            message:insertData.status == 1 ? '发布失败！':'保存失败！',
            type:'error'
          })
          res.end()
      }else{
        res.send({
          message:insertData.status == 1 ? '发布成功！':'保存成功！',
          type:'success'
        })
      }
  })
});

router.post('/addimage',function(req, res, next) {
  var form = new formidable.IncomingForm();
  form.on('field',(field,value)=>{
    // console.log(field);
    // console.log(value);
});
form.on('file',(name,file)=>{
    // console.log(name);
    // console.log(file);
});
form.on('end',()=>{
    // console.log(JSON.stringify(form.lookbehind))
    res.send(JSON.stringify(form.openedFiles[0].path))
})
     form.encoding = 'utf-8';
     form.uploadDir =path.join(resolve('./')+'/public/upload') ;
     form.keepExtensions = true;//保留后缀
     form.maxFieldsSize = 2 * 1024 * 1024;
    // //处理图片
    form.parse(req, function (err, fields, files){
      // let extname = path.extname(files.file.name);
      // let oldpath=form.uploadDir+'/'+files.file.path
      // let newpath = form.uploadDir + '/uploads/' + ttt + ran + extname;
    })
});
module.exports = router;